package com.hvgroup.modules.sanic.service.impl.chat;

import com.hvgroup.modules.sanic.service.impl.chat.chat.EventHandler;
import com.hvgroup.modules.sanic.service.impl.chat.template.ExecuteTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class EventStrategyImpl implements EventStrategy {

    private final Map<Integer, List<EventHandler>> source = new ConcurrentHashMap<>();
    private final Map<Integer, ExecuteTemplate> templateMap = new ConcurrentHashMap<>();

    public EventStrategyImpl(List<EventHandler> list1, List<ExecuteTemplate> list2) {
        for (EventHandler handler : list1) {
            if (!source.containsKey(handler.getType().getValue())) {
                source.put(handler.getType().getValue(), new ArrayList<>());
            }
            source.get(handler.getType().getValue()).add(handler);
        }

        list2.forEach(handler -> templateMap.put(handler.getType().getValue(), handler));
    }

    @Override
    public List<EventHandler> getHandlers(Integer type) {
        return source.get(type);
    }

    @Override
    public ExecuteTemplate getExecute(Integer type) {
        return templateMap.get(type);
    }

}
